package vn.edu.saigontech.ncms.web.offering.report;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.VideoSchedule;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.DateUtil;

public class StudyingScheduleReport {
	private LinkedList<Date> range = new LinkedList<Date>();
	private List<DayScheduleDetail> daySchedules = new ArrayList<DayScheduleDetail>();

	public StudyingScheduleReport(Date fromDate, Date toDate) {
		this.range = new LinkedList<Date>(DateUtil.sequenceAsList(fromDate, toDate));
		this.init();
	}
	
	private void init() {
		Collection<Offering> offerings = OfferingService.INSTANCE.findByDate(this.range.getFirst(), this.range.getLast());
		for(Date date : this.range) {
			DayScheduleDetail daySchedule = new DayScheduleDetail(date);
			for(Offering offering : offerings) {
				//System.out.println(offering.getName() + " - " + date);
				//if(offering.checkExistStudyingSchedule(date)) {
				if(offering.checkExistVideoScheduleNTest(date)) {					
					//System.out.println("This class is OK!!!!");
					OfferingScheduleDetail offeringSchedule = new OfferingScheduleDetail(offering);
//					System.out.println(offeringSchedule + " --- " + date);
//					System.out.println(offering.getName() + " --- " + offering.getTeacher(date));
					Teacher teacher = offering.getTeacher(date);
					if (teacher != null) offeringSchedule.setInstructor(teacher.getFullName());
					VideoSchedule video = offering.getVideoSchedule(date); 
					if(video != null) {
						offeringSchedule.setVideo(video.getVideo());
						offeringSchedule.setNote(video.getComment());
					}
					String testTitle = offering.getTest(date);
					if(testTitle != null) {
						offeringSchedule.setTest(testTitle);
						offeringSchedule.setNote("");
					}
					daySchedule.addOfferingScheduleDetail(offeringSchedule);
				}
			}
			this.addDayScheduleDetail(daySchedule);
		}
	}
	
	public void addDayScheduleDetail(DayScheduleDetail detail) {
		this.daySchedules.add(detail);
	}

	public List<DayScheduleDetail> getDaySchedules() {
		return daySchedules;
	}

	public void setDaySchedules(List<DayScheduleDetail> daySchedules) {
		this.daySchedules = daySchedules;
	}

	public LinkedList<Date> getRange() {
		return range;
	}

	public void setRange(LinkedList<Date> range) {
		this.range = range;
	}
}
